package fileloaders.parsers;

import java.util.HashSet;
import java.util.Hashtable;
import java.util.regex.Pattern;

import puppy.graph.AgeAggregator;
import puppy.graph.DeliciousNode;

public class InLineParser {

	/**
	 * @param args
	 */
	
	static private Pattern tab =Pattern.compile("\t");
	
	static private Pattern comma =Pattern.compile(",");
	static private Pattern p_bigram_a =Pattern.compile("\\),\\(");
	
	static private Pattern graph_a= Pattern.compile("\\),\\(");
	
	public static Hashtable<String,Object> parseBigramEnrichedLine(String line[]){
		
	
		
		String[] keys = new String[2];
//		0	news	
//   {(adults,0,news,6,7355624,8.157023795669817E-7),
//(kids-teens,0,news,1,147564,6.776720609362717E-6)
//		(teens-mteens,0,news,1,855929,1.1683212042120316E-6)}

		
		keys[0]= new String(line[0]);
		keys[1] = new String(line[1]);
		boolean foundvalid=true;
	
		Hashtable<String,Object> r= new Hashtable<String,Object>();
		//url, tag, freq_tag, freq_url, freq_ut, type, f_t, f_u,domain 
		r.put("keys", keys);	
		
		
		
		String items[] =  p_bigram_a.split(line[2]);
		for(int i =0 ; i<items.length; i++){
			
		

			String temp[]= comma.split(items[i]);
		
			String age = new String(temp[0]).replace("{(", "");
		//	if (!age.equals("adults")){
				
			//	foundvalid=true;
		//	}
				
		
			Integer freq= Integer.valueOf(new String(temp[3])); 
		//	System.out.println(keys[0]+"\t"+ keys[1]+ "\t"+ age + "\t"+ freq);
			r.put(age, freq);
			
			
			
		}
		
		if (foundvalid == true) {
			return r;
		} else {
			return null;
		}
		
		
	}
	
	
	
	public static Hashtable<String,Object[]> parseGraphEnrichedLine(String line[]){
		
		//(http://ytv.com/,station)       
		//{(http://ytv.com/,station,3,8,1,kids-teens,25924,8,ytv.com),
		//(http://ytv.com/,station,1000,8,1,adults,25924,8,ytv.com)}
		int index = line[0].lastIndexOf(",");
		String[] keys = new String[2];
	
		
		keys[0]= new String(line[0].substring(1,index));
		keys[1] = new String(line[0].substring(index+1,line[0].length()-1));
		
		Hashtable<String,Object[]> r= new Hashtable<String,Object[]>();
		//url, tag, freq_tag, freq_url, freq_ut, type, f_t, f_u,domain 
		r.put("keys", keys);
		
		//System.out.println(line[1]);
		line[1]= line[1].replace(keys[0], "");
		//line[1]= line[1].replaceAll(keys[1], "");
		String items[] = graph_a.split(line[1]);
		
	//	System.out.println(line[1]);
		for(int i =0 ; i<items.length; i++){
			
			Integer[] a = new Integer[5];

			String temp[]= comma.split(items[i]);
		
			String age = new String(temp[5]);
			
			a[0]= Integer.valueOf(new String(temp[2])); 
			a[1]= Integer.valueOf(new String(temp[3])); 
			a[2]= Integer.valueOf(new String(temp[4])); 
			a[3]= Integer.valueOf(new String(temp[6])); 
			a[4]= Integer.valueOf(new String(temp[7])); 
			
			r.put(age, a);
			
			
			
		}
		return r;
	
	}
	
	
	
	public static Hashtable<String,Object[]> 
	     parseGraphEnrichedLineAgg(String line[], AgeAggregator agg, Hashtable<String, DeliciousNode> nodes){
		
		//(http://ytv.com/,station)       
		//{(http://ytv.com/,station,3,8,1,kids-teens,25924,8,ytv.com),
		//(http://ytv.com/,station,1000,8,1,adults,25924,8,ytv.com)}
		int index = line[0].lastIndexOf(",");
		String[] keys = new String[2];

		
		keys[0]= new String(line[0].substring(1,index));
		keys[1] = new String(line[0].substring(index+1,line[0].length()-1));
		
		Hashtable<String,Object[]> r= new Hashtable<String,Object[]>();
		//url, tag, freq_tag, freq_url, freq_ut, type, f_t, f_u,domain 
		r.put("keys", keys);
		
		//System.out.println(line[1]);
		line[1]= line[1].replace(keys[0], "");
		//line[1]= line[1].replaceAll(keys[1], "");
		String items[] = graph_a.split(line[1]);
		
//		System.out.println(line[1]);
		Float[] a = new Float[5];
		a[0]=a[1]=a[2]=a[3]=a[4]=0f;
		for(int i =0 ; i<items.length; i++){
			
			String temp[]= comma.split(items[i]);
		
			String age = new String(temp[5]);
				
				if(!agg.isAgeValid(age)) continue;
				
				//aggregate tag frequency if we have nnot use the sum yet
				
				a[0]= a[0]+ Float.valueOf(new String(temp[2])); //freq tag
				a[1]= a[1]+ Float.valueOf(new String(temp[3])); //freq url
				
				//this always is added
				a[2]= a[2]+ Float.valueOf(new String(temp[4]));  //frq ut
				
				//do not aggregate adult freqs
				a[3]= Float.valueOf(new String(temp[6]));  // freq t adults
				a[4]= Float.valueOf(new String(temp[7]));  // freq u adults
				
		
		}
		r.put("kids", a);
		return r;
		
		}
	
	     
	     
	     
	public static Hashtable<String, Object> parseGraphEnrichedLine(
			String line[], AgeAggregator agg,
			Hashtable<String, HashSet<String>> ages,
			Hashtable<String, DeliciousNode> nodes) {

		// (http://ytv.com/,station)
		// {(http://ytv.com/,station,3,8,1,kids-teens,25924,8,ytv.com),
		// (http://ytv.com/,station,1000,8,1,adults,25924,8,ytv.com)}
		int index = line[0].lastIndexOf(",");
		String[] keys = new String[2];

		keys[0] = new String(line[0].substring(1, index));
		keys[1] = new String(line[0].substring(index + 1, line[0].length() - 1));

		Hashtable<String, Object> r = new Hashtable<String, Object>();
		// url, tag, freq_tag, freq_url, freq_ut, type, f_t, f_u,domain
		r.put("keys", keys);

		// System.out.println(line[1]);
		line[1] = line[1].replace(keys[0], "");
		// line[1]= line[1].replaceAll(keys[1], "");
		String items[] = graph_a.split(line[1]);

		// System.out.println(line[1]);
		Float[] a = new Float[2];
		a[0] = a[1] = 0f;

		Float link_freq = 0f;
		DeliciousNode tag = null;

		DeliciousNode url = null;
		for (int i = 0; i < items.length; i++) {

			String temp[] = comma.split(items[i]);

			String age = new String(temp[5]);
			
			if (!agg.isAgeValid(age))
				continue;

			// aggregate tag frequency if we have nnot use the sum yet

			a[0] = Float.valueOf(new String(temp[2])); // freq tag

			if (nodes.containsKey(keys[1])) {
				tag = nodes.get(keys[1]);
				if (!ages.get(keys[1]).contains(age)) {

					tag.setProb(tag.getProb() + a[0]);
					ages.get(keys[1]).add(age);
				}
			} else {

				tag = new DeliciousNode();
				tag.setProb(a[0]);
				HashSet<String> set = new HashSet<String>();
				set.add(age);
				tag.setType(false);
				tag.setName(keys[1]);
				ages.put(keys[1], set);

			}

			a[1] = a[1] + Float.valueOf(new String(temp[3])); // freq url

			// for url

			if (nodes.containsKey(keys[0])) {
				url = nodes.get(keys[0]);
				if (!ages.get(keys[0]).contains(age)) {

					url.setProb(url.getProb() + a[1]);
					ages.get(keys[0]).add(age);
				}
			} else {

				url = new DeliciousNode();
				url.setProb(a[1]);
				HashSet<String> set = new HashSet<String>();
				set.add(age);
				url.setType(true);
				url.setName(keys[0]);
				ages.put(keys[0], set);

			}

			// this always is added
			link_freq = link_freq + Float.valueOf(new String(temp[4])); // frq
																		// ut

			// do not aggregate adult freqs

			url.setProb_adults(Float.valueOf(new String(temp[7])));
			tag.setProb_adults(Float.valueOf(new String(temp[6])));

		}
		
		
		if(tag==null && url==null)return null;
		//System.out.println(tag + "tag \t url" + url);
		nodes.put(keys[1], tag);
		nodes.put(keys[0], url);
		r.put("freq_ut", link_freq);
		return r;

	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		String test="(http://alfy.com/,games)	{(http://alfy.com/,games,188564,6,1,adults,1285688,6,alfy.com),(http://alfy.com/,games,11503,6,1,kids,1285688,6,alfy.com)}";
		parseGraphEnrichedLine(test.split("\t"));
		
		
		test="0	news	{(adults,0,news,6,7355624,8.157023795669817E-7),(kids-teens,0,news,1,147564,6.776720609362717E-6)}";
		parseBigramEnrichedLine(test.split("\t"));
	}

}
